package com.jyxd.web.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.Iterator;
import java.util.Map;

import static com.jyxd.web.config.ConstantConfig.SORT_ASC;
import static com.jyxd.web.config.ConstantConfig.SORT_DESC;

public class MybatisPlusUtil {
    public static <T> QueryWrapper<T> getParamOneOrderByAsc(String paramKey, Object paramValue, String status, String column) {
        return new QueryWrapper<T>().eq(paramKey, paramValue).eq("status", status).orderByAsc(column);
    }

    public static <T> QueryWrapper<T> getParamOneOrderByDesc(String paramKey, Object paramValue, String status, String column) {
        return new QueryWrapper<T>().eq(paramKey, paramValue).eq("status", status).orderByDesc(column);
    }

    public static <T> QueryWrapper<T> getParamOne(String paramKey, Object paramValue, String status) {
        return new QueryWrapper<T>().eq(paramKey, paramValue).eq("status", status);
    }

    public static <T> QueryWrapper<T> getParamOne(String paramKey, Object paramValue) {
        return new QueryWrapper<T>().eq(paramKey, paramValue);
    }

    public static <T> QueryWrapper<T> getParamList(Map<String, Object> map, String sortRule, String column) {
        QueryWrapper<T> tQueryWrapper = getParamList(map);
        if (StringUtils.equals(sortRule, SORT_ASC))
            tQueryWrapper.orderByAsc(column);
        if (StringUtils.equals(sortRule, SORT_DESC))
            tQueryWrapper.orderByDesc(column);
        return tQueryWrapper;
    }

    public static <T> QueryWrapper<T> getParamList(Map<String, Object> map) {
        QueryWrapper<T> tQueryWrapper = new QueryWrapper<>();
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            tQueryWrapper.eq(entry.getKey(), entry.getValue());
        }
        return tQueryWrapper;
    }
}
